﻿using AutoMapper;
using MediatR;
using Traceability.App.Api.Read.Application.Command.Health;
using Traceability.App.Domain;
using Traceability.App.Domain.新文件夹;
using Traceability.App.ErrorCode;
using Traceability.App.Intrastructure.Interface;

namespace Traceability.App.Api.Read.Application.Handler.Health
{
    public class HealthDelHandler : IRequestHandler<HealthDelCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<HealthModel> healthRepo;
        private readonly IMapper mapper;

        public HealthDelHandler(IBaseRepository<HealthModel>healthRepo,IMapper mapper)
        {
            this.healthRepo = healthRepo;
            this.mapper = mapper;
        }
        public Task<ApiResult<int>> Handle(HealthDelCommand request, CancellationToken cancellationToken)
        {
            var res = new ApiResult<int>();
            var trace = healthRepo.GetValue(request.Id).Result;
            if (trace == null)
            {
                res.Message = "用户不存在";
                res.Code = ApiEnum.Error;
                return Task.FromResult(res);
            }
            var entity = mapper.Map<HealthModel>(trace);
            entity.IsDel = true;
            var r = healthRepo.Update(entity).Result;
            if (r > 0)
            {
                res.Message = "删除成功";
                res.Code = ApiEnum.Ok;
                res.Data = r;
            }
            else
            {
                res.Message = "删除失败";
                res.Code = ApiEnum.Error;
                res.Data = r;
            }
            return Task.FromResult(res);
        }
    }
}
